package com.cd.university.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cd.university.common.RestResult;
import com.cd.university.controller.form.ProjectStIdAndDeIdForm;
import com.cd.university.controller.form.ProjectForm;
import com.cd.university.controller.form.ProjectIdForm;
import com.cd.university.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 陈栋
 * @create 2021/10/15 16:46
 */
@RestController
@RequestMapping("/project")
@Api("项目接口")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @PostMapping("/apply")
    @ApiOperation("项目申请")
    @PreAuthorize("hasAnyAuthority('教师','超级管理员')")
    public RestResult projectApply(@RequestBody ProjectForm form) {
        return projectService.projectApply(form);
    }

    @GetMapping("/advance")
    @ApiOperation("预先申请创建一个项目")
    @PreAuthorize("hasAnyAuthority('教师','超级管理员')")
    public RestResult advanceProject() {
        return projectService.advanceInsertProject();
    }

    @GetMapping("/")
    @ApiOperation("根据用户id搜索项目")
    public RestResult searchProject(@RequestParam Integer userId,
                                    @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                    @RequestParam(required = false,defaultValue = "6") Integer pageSize) {
        return projectService.searchProjectByUserId(userId,pageNum,pageSize);
    }

    @PostMapping("/getProjectInfoList")
    @ApiOperation("根据项目id查询项目的所有信息")
    public RestResult getProjectInfoList(@RequestBody JSONObject jsonObject) {
        // 获得ProjectIdForm集合
        JSONArray jsonList = jsonObject.getJSONArray("projectList");
        // 把JSONArray转成Java的List的projectIdForms对象
        List<ProjectIdForm> projectIdForms = jsonList.toJavaList(ProjectIdForm.class);

        // 获得分页参数
        Integer pageNum = Integer.valueOf((String) jsonObject.get("pageNum"));
        // 如果没有传入pageNum，那么默认为1
        pageNum = pageNum==null?1:pageNum;
        Integer pageSize = Integer.valueOf((String) jsonObject.get("pageSize"));
        // 如果没有传pageSize,那么默认为6
        pageSize = pageSize==null?6:pageSize;

        return projectService.searchProjectList(projectIdForms,pageNum,pageSize);
    }

    @PostMapping("/getProjetByInfo")
    @ApiOperation("根据项目的信息(状态id或者部门id)查询用户的项目")
    public RestResult getProjectByStIdAndDeId(@Valid @RequestBody ProjectStIdAndDeIdForm form) {
        return projectService.getProjectByStIdAndDeId(form.getUserId(),
                form.getStatusId(),
                form.getDepartmentId(),
                form.getPageNum(),
                form.getPageSize());
    }

    @GetMapping("/getAllProjects")
    @ApiOperation("获得所有的项目")
    public RestResult getAllProjects(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false,defaultValue = "6") Integer pageSize) {
        return projectService.searchAllProject(pageNum,pageSize);
    }

    @GetMapping("/getProjectByDepartmentAndUserId")
    @ApiOperation("根据用单位id和用户id搜索可以结题的项目")
    public RestResult getProjectByDepartmentAndUserId(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = false,defaultValue = "6") Integer pageSize,
                                                      @RequestParam(required = false) Integer userId,
                                                      @RequestParam(required = true) Integer departmentId) {
        return projectService.getProjectByDepartmentAndUserId(pageNum, pageSize, userId, departmentId);
    }

    @GetMapping("/getProjectByDepartment")
    @ApiOperation("根据单位id(可以携带用户id)搜索已经结题的项目")
    public RestResult getProjectByDepartmentId(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                               @RequestParam(required = false,defaultValue = "6") Integer pageSize,
                                               @RequestParam(required = true) Integer userId,
                                               @RequestParam(required = true) Integer departmentId) {
        return projectService.getProjectByDepartmentId(pageNum, pageSize, userId, departmentId);
    }

    @GetMapping("/getProjectByName")
    @ApiOperation("根据项目的名称搜索结题项目")
    public RestResult getProjectByName(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                       @RequestParam(required = false,defaultValue = "6") Integer pageSize,
                                       @RequestParam(required = false) Integer userId,
                                       @RequestParam(required = true) String projectName) {
        return projectService.getProjectByName(pageNum, pageSize, userId, projectName);
    }

}
